﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.DAL;
using Telerik.Web.UI;


namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class TR_Trainers : System.Web.UI.UserControl
    {
        private object _dataItem = null;
        private InitialData_BLL iniData = new InitialData_BLL();
        private MasNamePrefixBLL namePre = new MasNamePrefixBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        TR_TrainerDistrictInfo distinfo = new TR_TrainerDistrictInfo();
        TR_TrainerDistrictDAL distdal = new TR_TrainerDistrictDAL();
        private EmployeeInfo empInfo;
        //ReadOnly property.
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                bindEmp();
                bindPrefixName();                

                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblID.Text = DataBinder.Eval(DataItem, "Sequence").ToString();

                    if (DataBinder.Eval(DataItem, "emp_seq").ToString() != "")// in
                    {
                        checkStaEmp("in");
                        IList<EmployeeInfo> empList = empBLL.ListEmployees(cmbEmployee.SelectedValue);
                        if (empList.Count > 0)
                        {
                            cmbEmployee.SelectedValue = DataBinder.Eval(DataItem, "emp_seq").ToString();
                            empInfo = empList[0];
                            hidPrefix.Value = empInfo.Person.NamePrefix.Sequence.ToString();
                            lblPrefix.Text = empInfo.Person.NamePrefix.Name;
                            txtName.Text = empInfo.Person.FirstName != null ? empInfo.Person.FirstName : "";
                            txtLastName.Text = empInfo.Person.LastName != null ? empInfo.Person.LastName : "";
                            txtAddress.Text = empInfo.Address != null ? empInfo.Address.Address : "";
                        }
                    }
                    else
                    {
                        checkStaEmp("out");
                        ddlPrefix.SelectedValue = DataBinder.Eval(DataItem, "name_prefix_seq").ToString();
                        txtName.Text = DataBinder.Eval(DataItem, "first_name").ToString();
                        txtLastName.Text = DataBinder.Eval(DataItem, "last_name").ToString();
                        txtAddress.Text = DataBinder.Eval(DataItem, "address").ToString();
                    }
                    txtPostCode.Text = DataBinder.Eval(DataItem, "postal_code").ToString();
                    txtTel.Text = DataBinder.Eval(DataItem, "tel").ToString();


                    
                    //hidDis.Value = DataBinder.Eval(DataItem, "district_seq").ToString();
                    //DataSet ds = new DataSet();
                    //ds = iniData.ListMasDistricts(hidDis.Value);
                    //txtDistrict.Text = hidDis.Value + "-" + ds.Tables[0].Rows[0]["Address"].ToString();

                    if (DataBinder.Eval(DataItem, "active").ToString() == "1")// พร้อม
                    {
                        cbAtiveSta.Checked = true;
                    }
                    else
                    {
                        cbAtiveSta.Checked = false;
                    }

                    this.btnAddnew.Visible = false;
                    this.btnUpdate.Visible = true;
                }
                else
                {
                    ddlPrefix.Visible = true;
                    this.btnUpdate.Visible = false;
                    this.btnAddnew.Visible = true;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ListDistrictComboBox();
        }

        protected void bindPrefixName()
        {
            IList<MasNamePrefixInfo> infoList = namePre.ListNamePrefixes();
            ddlPrefix.DataSource = infoList;
            ddlPrefix.DataBind();
        }

        //private void bindEmpType()
        //{
        //    IList<EmployeeTypeInfo> infoList = empTypeBLL.ListEmployeeTypes();
        //    ddlEmployeeType.DataSource = infoList;
        //    ddlEmployeeType.DataBind();
        //}

        protected void bindEmp()
        {
            IList<EmployeeInfo> infoList = empBLL.ListEmployeeByTypes("", string.Empty, string.Empty);

            foreach (EmployeeInfo List in infoList)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = List.Person.NamePrefix.Name + List.Person.FirstName + " " + List.Person.LastName;
                item.Value = List.Sequence.ToString();

                item.Attributes.Add("EMP_NAME", List.Person.NamePrefix.Name + List.Person.FirstName + " " + List.Person.LastName);
                item.Attributes.Add("EMP_NAME_ENG", List.Person.FirstNameEng + " " + List.Person.LastNameEng);
                //item.Attributes.Add("POS_NAME", List.Position == null ? "" : List.Position.Name);
                //item.Attributes.Add("CLASS_NAME", List.Class.Name);

                cmbEmployee.Items.Add(item);
                item.DataBind();
            }
        }

        protected void checkStaEmp(string status)
        {
            if (status == "out")
            {
                rdoEmpIn.Checked = false;
                rdoEmpOut.Checked = true;
                ddlPrefix.Visible = true;
                lblPrefix.Visible = false;
                txtName.Enabled = true;
                txtLastName.Enabled = true;
                txtAddress.Enabled = true;
                //txtPostCode.Enabled = true;
                //txtTel.Enabled = true;
            }
            else
            {
                rdoEmpOut.Checked = false;
                rdoEmpIn.Checked = true;
                ddlPrefix.Visible = false;
                lblPrefix.Visible = true;
                txtName.Enabled = false;
                txtLastName.Enabled = false;
                txtAddress.Enabled = false;
                //txtPostCode.Enabled = false;
                //txtTel.Enabled = false;
            }
            txtName.Text = "";
            txtLastName.Text = "";
            txtAddress.Text = "";
            txtPostCode.Text = "";
            txtTel.Text = "";
        }
 
        

        protected void rdoEmpOut_CheckedChanged(object sender, EventArgs e)
        {
            checkStaEmp("out");
        }

        protected void btnSelectItem_Click(object sender, EventArgs e)
        {
            if (cmbEmployee.Text != "")
            {
                checkStaEmp("in");
                IList<EmployeeInfo> empList = empBLL.ListEmployees(cmbEmployee.SelectedValue);
                if (empList.Count > 0)
                {
                    empInfo = empList[0];
                    hidPrefix.Value = empInfo.Person.NamePrefix.Sequence.ToString();
                    lblPrefix.Text = empInfo.Person.NamePrefix.Name;
                    txtName.Text = empInfo.Person.FirstName != null ? empInfo.Person.FirstName : "";
                    txtLastName.Text = empInfo.Person.LastName != null ? empInfo.Person.LastName : "";
                    txtAddress.Text = empInfo.Address != null ? empInfo.Address.Address : "";
                    //txtDistrict.Text = empInfo.Address != null ? empInfo.Address.PostalCode : "";
                    txtPostCode.Text = empInfo.Address != null ? empInfo.Address.PostalCode : "";
                    txtTel.Text = empInfo.Address != null ? empInfo.Address.Phone : "";
                }
            }
            else 
            {
                checkStaEmp("out");
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            checkStaEmp("out");
        }
        public void ListDistrictComboBox()
        {
            cbDistrict.DataSource = distdal.GetFill();
            cbDistrict.DataBind();
            cbAmphurs.DataSource = distdal.GetFill();
            cbAmphurs.DataBind();
            //cbProvinces.DataSource = distdal.GetFill();
            //cbProvinces.DataBind();
            
        }
        protected void btnDistrict_Load(object sender, EventArgs e)
        {

            //btnDistrict.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogDistricts.aspx','../frmIframe.html',700,500)");
        }
        protected void btnDistrict_Click(object sender, ImageClickEventArgs e)
        {
            string st = Hide.Value;
            string[] str = st.Split('-');            //if (str[0] != "")
            //{
            //    hidDis.Value = str[0];
            //    txtDistrict.Text = st;
            //}
            //else
            //{
            //    txtDistrict.Text = "";
            //}

        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            if (rdoEmpIn.Checked == true)
            {
                //iniData.InsertTrainers("in", cmbEmployee.SelectedValue, "", "", "", "", hidDis.Value, txtTel.Text, txtPostCode.Text, cbAtiveSta.Checked == true ? "1" : "0", "1");
            }
            else 
            {
                //iniData.InsertTrainers("out", "", ddlPrefix.SelectedValue, txtName.Text, txtLastName.Text, txtAddress.Text, hidDis.Value, txtTel.Text, txtPostCode.Text, cbAtiveSta.Checked == true ? "1" : "0", "1");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (rdoEmpIn.Checked == true)
            {
                //iniData.UpdateTrainers("in", lblID.Text, cmbEmployee.SelectedValue, "", "", "", "", hidDis.Value, txtTel.Text, txtPostCode.Text, cbAtiveSta.Checked == true ? "1" : "0", "1");
            }
            else
            {
                //iniData.UpdateTrainers("out", lblID.Text, cmbEmployee.SelectedValue, ddlPrefix.SelectedValue, txtName.Text, txtLastName.Text, txtAddress.Text, hidDis.Value, txtTel.Text, txtPostCode.Text, cbAtiveSta.Checked == true ? "1" : "0", "1");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        

        

        

       

    }
}